package com.situ.car.dao.Impl;

import com.situ.car.dao.ICarDao;
import com.situ.car.pojo.entity.Car;
import com.situ.car.pojo.query.BrandCarQuery;
import com.situ.car.pojo.query.CarQuery;
import com.situ.car.pojo.vo.AdminVo;
import com.situ.car.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class CarDaoImpl implements ICarDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Car> selectByBrandCar(Integer brandId) {
        List<Car> list = template.query("SELECT car.*\n" +
                "FROM car\n" +
                "JOIN car_brand ON car.brand_id = car_brand.brand_id\n" +
                "WHERE status=1 and car_brand.brand_id = ?", new BeanPropertyRowMapper<>(Car.class), brandId);
        return list;
    }

    @Override
    public Car selectByCar(Integer id) {
        return template.queryForObject("SELECT id, license_plate, image, model, production_year AS year, color, details, status, rentStatus, rental_price, car_category_id AS categoryId, brand_id AS brandId\n" +
                "FROM car where id=?", new BeanPropertyRowMapper<>(Car.class), id);
    }

    @Override
    public void updateRentalStatus(Integer carId) {
        template.update("update car set rentStatus=0 where id=?", carId);
    }

    @Override
    public List<Car> selectByRentalPrice(Integer carId) {
        return template.query("select rental_price from car where id=?", new BeanPropertyRowMapper<>(Car.class), carId);
    }

    @Override
    public void updateCarStatus(Integer rentalId) {
        template.update("UPDATE car AS c\n" +
                "JOIN rental_order AS r ON c.id = r.car_id\n" +
                "SET c.rentStatus = 1\n" +
                "WHERE status=1 and r.id = ?", rentalId);
    }

    @Override
    public List<Car> selectByCarList() {
        List<Car> list = template.query("SELECT * FROM car WHERE status=1", new BeanPropertyRowMapper<>(Car.class));
        return list;
    }

    @Override
    public List<Car> selectByPage(CarQuery carQuery) {
        String sql = "SELECT * FROM car where status=1";
        List<Object> queryList = new ArrayList<>();
        String queryModel = carQuery.getModel();
        if (!ObjectUtils.isEmpty(queryModel)) {
            sql += " and model like ?";
            queryList.add("%" + queryModel + "%");
        }
        sql += " limit ?,?";
        int offset = (carQuery.getPage() - 1) * carQuery.getLimit();
        queryList.add(offset);
        queryList.add(carQuery.getLimit());
        List<Car> list = template.query(sql, new BeanPropertyRowMapper<>(Car.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(CarQuery carQuery) {
        String sql = "select count(*) FROM car where status=1";
        List<Object> queryList = new ArrayList<>();
        String queryModel = carQuery.getModel();
        if (!ObjectUtils.isEmpty(queryModel)) {
            sql += " and model like ?";
            queryList.add("%" + queryModel + "%");
        }
        Integer count = template.queryForObject(sql, Integer.class, queryList.toArray());
        return count;
    }

    @Override
    public List<Car> selectByCarPage(BrandCarQuery brandCarQuery) {
        String sql = "SELECT car.* FROM car\n" +
                "JOIN car_brand ON car.brand_id = car_brand.brand_id\n" +
                "WHERE status=1 ";
        List<Object> queryList = new ArrayList<>();
        Integer queryBrandId = brandCarQuery.getBrandId();
        sql += " and car_brand.brand_id = ?";
        queryList.add(queryBrandId);
        String queryModel = brandCarQuery.getModel();
        if (!ObjectUtils.isEmpty(queryModel)) {
            sql += " and car.model like ?";
            queryList.add("%" + queryModel + "%");
        }
        sql += " limit ?,?";
        int offset = (brandCarQuery.getPage() - 1) * brandCarQuery.getLimit();
        queryList.add(offset);
        queryList.add(brandCarQuery.getLimit());
        List<Car> list = template.query(sql, new BeanPropertyRowMapper<>(Car.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectCarTotalCount(BrandCarQuery brandCarQuery) {
        String sql = "SELECT count(*) FROM car\n" +
                "JOIN car_brand ON car.brand_id = car_brand.brand_id\n" +
                "WHERE status=1 ";
        List<Object> queryList = new ArrayList<>();
        Integer queryBrandId = brandCarQuery.getBrandId();
        sql += " and car_brand.brand_id = ?";
        queryList.add(queryBrandId);
        String queryModel = brandCarQuery.getModel();
        if (!ObjectUtils.isEmpty(queryModel)) {
            sql += " and car.model like ?";
            queryList.add("%" + queryModel + "%");
        }
        Integer count = template.queryForObject(sql, Integer.class, queryList.toArray());
        return count;
    }
}
